package com.example.script.portal.util;

import com.example.script.model.SysUser;
import com.example.script.portal.model.PortalUserDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {


    private static final Logger logger= LoggerFactory.getLogger(SecurityUtils.class);
    public static SysUser getCurrentUser(){
        SecurityContext context= SecurityContextHolder.getContext();
        Authentication authentication=context.getAuthentication();
        if (authentication.getPrincipal() instanceof  String){
            logger.info("用户名匿名登录，登录名为"+authentication.getPrincipal());
            return null;
        }
        PortalUserDetails portalSecurityConfig=(PortalUserDetails) authentication.getPrincipal();
        return portalSecurityConfig.getSysUser();
    }
}
